<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>按钮1</button>
    <button>按钮2</button>
    <button>按钮3</button>
    <button>按钮4</button>
    <button>按钮5</button>
    <script>
        /* 
            需求：需要给每个按钮都绑定onclick事件
            当点击当前按钮的时候把当前按钮的backgroundColor设置为红色，并且
            把其它的按钮的背景色去掉。
        */
        const btnsObj = document.querySelectorAll("button");
        for (const btn of btnsObj) {
            btn.onclick = function () {
                setAllBackgroundColor(btnsObj);
                this.style.backgroundColor = 'red';
            }
        }

        function setAllBackgroundColor(btnsObj) {
            for (const btn of btnsObj) {
                btn.style.backgroundColor = '';
            }
        }
    </script>
</body>

</html>